﻿#include "OperLoads.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKLoadExcitationForceMoments.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsLoadManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKLoadPowerSourcesSEABeam.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "updateBrowserTable.h"
#include "GUIWidget/AttributeSetDialog.h"

#include <QMessageBox>

namespace ModelOper
{
    bool OperLoads::execGUI()
    {
        return false;
    }
    bool OperLoads::execProfession()
    {
        return false;
    }
    void OperLoads::editLoad(const int loadID)
    {
        if (!_loadMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        QObject object;
        object.setObjectName("actionEditLoad");
        Core::FITKActionOperator* oper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>("actionEditLoad");
        if (!oper) return;
        oper->setArgs("DataID", loadID);
        oper->setEmitter(&object);
        oper->actionTriggered();
    }

    void OperLoads::removeLoad(const int loadID)
    {
        //获取管理器
        if (!_loadMgr)return;
        //移除并更新列表
        _loadMgr->removeDataByID(loadID);
        ModelOper::updateBrowserTable();
        EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        operGraph->updateLoads();
    }

    void OperLoads::setAttribute(const QString & name, const QString & currentText, const int attrIndex, const QList<int>& loadIDs)
    {
        //获取管理器
        if (!_loadMgr)return;
        Acoustics::FITKAcousticsAbstractLoad* load = dynamic_cast<Acoustics::FITKAcousticsAbstractLoad* >(_loadMgr->getDataByID(loadIDs[0]));
        if (!load)return;
        //根据拾取列表的第一个元素ID确定载荷类型
        Acoustics::FITKAcousticsAbstractLoad::AcousticsLoadType type = load->getAcousticsLoadType();

        if (type == Acoustics::FITKAcousticsAbstractLoad::ExcitationForceMoments)
            setAttributePointForcesMoments(name, currentText, attrIndex, loadIDs);
        else if (type == Acoustics::FITKAcousticsAbstractLoad::ConstraintSEABeam)
            setAttributeConstraintBeam(name, currentText, attrIndex, loadIDs);
        else if (type == Acoustics::FITKAcousticsAbstractLoad::ConstraintSEAShell)
            setAttributeConstraintShell(name, currentText, attrIndex, loadIDs);
        else if (type == Acoustics::FITKAcousticsAbstractLoad::ConstraintSEAAcoustic)
            setAttributeConstraintAcoustic(name, currentText, attrIndex, loadIDs);
        else if (type == Acoustics::FITKAcousticsAbstractLoad::PowerSEABeam)
            setAttributePowerBeam(name, currentText, attrIndex, loadIDs);
        else if (type == Acoustics::FITKAcousticsAbstractLoad::PowerSEAShell)
            setAttributePowerShell(name, currentText, attrIndex, loadIDs);
        else if (type == Acoustics::FITKAcousticsAbstractLoad::PowerSEAAcoustic)
            setAttributePowerAcoustic(name, currentText, attrIndex, loadIDs);
        else if (type == Acoustics::FITKAcousticsAbstractLoad::ExcitationMonopole)
            setAttributeMonopole(name, currentText, attrIndex, loadIDs);

        //刷新表格
        ModelOper::updateBrowserTable();
    }

    void OperLoads::setAttributePointForcesMoments(const QString & name, const QString & currentText, const int attrIndex, const QList<int>& loadIDs)
    {
        if (!_loadMgr||!_specMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //设置对话框
        GUI::AttributeSetDialog dlg(2, mw);
        dlg.setTextLabel(name);
        //填充下拉列表
        QList<QString> list;
        list += "None";
        if (attrIndex == 3) {
            QList<Acoustics::FITKAcousticsAbstractSpectra*> specList = _specMgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Force);
            for (auto value : specList) 
                list += value->getDataObjectName();
        }
        else if (attrIndex == 4) {
            QList<Acoustics::FITKAcousticsAbstractSpectra*> specList = _specMgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Moment);
            for (auto value : specList)
                list += value->getDataObjectName();
        }
        dlg.setDefaultValueQString(list);

        if (dlg.exec() != QDialog::Accepted) return;

        QString specName = dlg.getValueQString();
        int specID = -1;
        if (_specMgr->getDataByName(specName)) {
            specID = _specMgr->getDataByName(specName)->getDataObjectID();
        }

        for (int id : loadIDs)
        {
            Acoustics::FITKLoadExcitationForceMoments* load = dynamic_cast
                <Acoustics::FITKLoadExcitationForceMoments*>(_loadMgr->getDataByID(id));
            if (load == nullptr) continue;
            //不同列设置不同参数

            switch (attrIndex)
            {
            case 3: //Forces
                load->setForceSpectraID(specID); break;
            case 4: //Moments
                load->setMomentSpectraID(specID); break;
            default: break;
            }
        }
    }

    void OperLoads::setAttributeConstraintAcoustic(const QString & name, const QString & currentText, const int attrIndex, const QList<int>& loadIDs)
    {
    }

    void OperLoads::setAttributeConstraintBeam(const QString & name, const QString & currentText, const int attrIndex, const QList<int>& loadIDs)
    {
    }

    void OperLoads::setAttributeConstraintShell(const QString & name, const QString & currentText, const int attrIndex, const QList<int>& loadIDs)
    {
    }

    void OperLoads::setAttributePowerAcoustic(const QString & name, const QString & currentText, const int attrIndex, const QList<int>& loadIDs)
    {
    }

    void OperLoads::setAttributePowerBeam(const QString & name, const QString & currentText, const int attrIndex, const QList<int>& loadIDs)
    {
        if (!_loadMgr || !_specMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //设置对话框
        GUI::AttributeSetDialog dlg(2, mw);
        dlg.setTextLabel(name);
        //填充下拉列表
        QList<QString> list;
        list += "None";
        QList<Acoustics::FITKAcousticsAbstractSpectra*> specList = _specMgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Power);
        for (auto value : specList)
            list += value->getDataObjectName();
        dlg.setDefaultValueQString(list);

        if (dlg.exec() != QDialog::Accepted) return;

        QString specName = dlg.getValueQString();
        int specID = -1;
        if (_specMgr->getDataByName(specName)) {
            specID = _specMgr->getDataByName(specName)->getDataObjectID();
        }

        for (int id : loadIDs)
        {
            Acoustics::FITKLoadPowerSourcesSEABeam* load = dynamic_cast
                <Acoustics::FITKLoadPowerSourcesSEABeam*>(_loadMgr->getDataByID(id));
            if (load == nullptr) continue;
            //不同列设置不同参数
            switch (attrIndex)
            {
            case 3:
                load->setFlexureXSpectraID(specID); break;
            case 4:
                load->setFlexureYSpectraID(specID); break;
            case 5:
                load->setExtensionZSpectraID(specID); break;
            case 6:
                load->setTorsionZZSpectraID(specID); break;
            default: break;
            }
        }
    }

    void OperLoads::setAttributePowerShell(const QString & name, const QString & currentText, const int attrIndex, const QList<int>& loadIDs)
    {
    }

    void OperLoads::setAttributeMonopole(const QString & name, const QString & currentText, const int attrIndex, const QList<int>& loadIDs)
    {
    }

}